package org.springforge.forgesvn.impl;

import java.util.Collection;


import org.springforge.forgesvn.SVNService;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;


public class SVNServiceImpl implements SVNService {

    
    public String getListEntries(String svn_path) throws SVNException {
    	   
    	FSRepositoryFactory.setup();
    	SVNRepository repository = SVNRepositoryFactory.create( SVNURL.parseURIDecoded(svn_path ) );;
        StringBuffer sb = new StringBuffer();
        SVNNodeKind nodeKind = repository.checkPath( "" ,  -1 );
        String message="Repository Root: " + repository.getRepositoryRoot( true );
        listEntries( repository , "" );
        return  message+"<br/><br/>"+listEntries( repository , "" );
    }

    public static String listEntries( SVNRepository repository, String path ) throws SVNException {
        Collection entries = repository.getDir( path, -1 , null , (Collection) null );
        java.util.Iterator iterator = entries.iterator( );
        String retour="";
        while ( iterator.hasNext( ) ) {
            SVNDirEntry entry = ( SVNDirEntry ) iterator.next( );
            retour=retour+ "/" + (path.equals( "" ) ? "" : path + "/" ) + entry.getName( ) + 
                               " ( author: '" + entry.getAuthor( ) + "'; revision: " + entry.getRevision( ) + 
                               "; date: " + entry.getDate( ) + ")<br/>" ;
            if ( entry.getKind() == SVNNodeKind.DIR ) {
            	retour=retour+listEntries( repository, ( path.equals( "" ) ) ? entry.getName( ) : path + "/" + entry.getName( ) );
            }
        }
        return retour;
    }
    
}
